#include "Shooter_PCH.h"
#include "Game.h"

/*--------------------------------------------------------------------------*/

bool			Game::doLoop		= false;
SDL_Window*		Game::sdlWindow		= NULL;
SDL_GLContext	Game::sdlGlContext	= NULL;

TexManager*		Game::texManager	= NULL;
MeshManager*	Game::meshManager	= NULL;

Vertex triangle[3];
VertexBuffer* bufor = NULL;
VertexArray* arr = NULL;
GLSLProgram* prog = NULL;

float anyvalue = 0.2f;
Material* material = NULL;
glm::mat4x4 mvp;
/*--------------------------------------------------------------------------*/

void Game::Init()
{
	std::cout << "Init start." << std::endl;

	SDL_Init(SDL_INIT_VIDEO);
	sdlWindow = SDL_CreateWindow(	"An SDL2 window",					// window title
									SDL_WINDOWPOS_CENTERED,				// initial x position
									SDL_WINDOWPOS_CENTERED,				// initial y position
									640,								// width, in pixels
									480,								// height, in pixels
									SDL_WINDOW_OPENGL					// flags - see below
									);
		
	if (sdlWindow == NULL) 
	{
		std::cout << "Could not create window: " << SDL_GetError() << std::endl;
		return;
	}

	sdlGlContext = SDL_GL_CreateContext(sdlWindow);
	
	if (sdlGlContext == NULL)
	{
		std::cout << "Failed to create GL context: " << SDL_GetError() << std::endl;
		return;
	}

	if (glewInit())
	{
		std::cout << "Failed to initialize GLEW." << std::endl;
		return;
	}

	doLoop = true;

	std::cout << "Initializing managers" << std::endl;
	texManager = new TexManager();

	std::cout << "Init done" << std::endl;

	/*--------------------------------------------------------------------------*/

	triangle[0].position = glm::vec3(-1, -1, 0);
	triangle[0].texcoord = glm::vec2(0, 1);

	triangle[1].position = glm::vec3(1, -1, 0);
	triangle[1].texcoord = glm::vec2(1, 1);

	triangle[2].position = glm::vec3(0, 1, 0);
	triangle[2].texcoord = glm::vec2(1, 0);

	bufor = new VertexBuffer(3);
	bufor->SetVertexData(triangle, 0, 3);
	
	arr = new VertexArray();
	arr->AttachVertexBuffer(bufor);

	prog = new GLSLProgram("../Data/glsl/default");

	glViewport(0, 0, 640, 480);
	glDisable(GL_CULL_FACE);

	/*--------------------------------------------------------------------------*/

	GetTexManager()->Load("replaceme.png");
	GetTexManager()->Load("rock1.jpg");

	material = new Material("default");

	float value = 0.5f;
	int imageIndex = 0;
	material->SetUniform(EUniform::PtnFloat1, "blue", &anyvalue);
	material->SetUniform(EUniform::Sampler, "image", &imageIndex);
	material->SetUniform(EUniform::PtnFloat4x4, "MVP", glm::value_ptr(mvp));
}

/*--------------------------------------------------------------------------*/

void Game::Loop()
{
	Input::PollEvents();

	mvp = glm::mat4x4();

	mvp *= glm::rotate( ((float)SDL_GetTicks()/40), 
						glm::vec3(	glm::sin((float)SDL_GetTicks()/100), 
						1.0f, 
						glm::cos((float)SDL_GetTicks()/100) ) );

	material->Bind();

	GetTexManager()->Bind("rock1.jpg", 0);

	arr->Bind();
	glDrawArrays(GL_TRIANGLES, 0, 3);

	SDL_PumpEvents();

	anyvalue = (glm::sin((float)SDL_GetTicks()/1000) * 0.5f) + 0.5f;
	/*--------------------------------------------------------------------------*/

	SDL_GL_SwapWindow(sdlWindow);

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT);
}

/*--------------------------------------------------------------------------*/

void Game::Finalize()
{
	std::cout << "Finalize start." << std::endl;

	SDL_DestroyWindow(sdlWindow);
	SDL_Quit();

	std::cout << "Finalize done." << std::endl;
}

/*--------------------------------------------------------------------------*/

bool Game::DoLoop()
{
	return doLoop;
}

/*--------------------------------------------------------------------------*/

TexManager* Game::GetTexManager()
{
	return texManager;
}

/*--------------------------------------------------------------------------*/

MeshManager* Game::GetMeshManager()
{
	return meshManager;
}

/*--------------------------------------------------------------------------*/
